package com.bluedon.ism.commons.dao;

import com.bluedon.ism.commons.entity.AccountRole;
import com.bluedon.ism.commons.entity.Role;
import com.bluedon.ism.commons.util.Constants;
import com.bluedon.ism.commons.util.Pager;
import com.bluedon.ism.commons.util.StringUtil;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Repository(value = "roleDAO")
public class RoleDAO extends BaseDAO {

	public Role addRole(Role role) {
		this.save(role);
		return role;
	}
	
	public void addAccountRole(AccountRole accountRole) {
	    save(accountRole);
	}

	public void updateRole(Role role) {
		this.update(role);
	}
	
	public void deleteRole(long id) {
		Role role = this.getRoleById(id);
		if (role != null && !role.getIsSystem()) {
			this.delete(role);
		}
	}

	public Role getRoleById(long id) {
		return (Role) findObjectById(Role.class, id);
	}
	
	private Pager<Role> getRolePage(int type, int pageNo, int pageSize, String roleName, String oid) {
		Map<String,Object> param = new HashMap<String,Object>();
		StringBuffer hql = new StringBuffer("from Role r where 1=1 and r.type = " + type);
//		if (type > -1) {
//			hql.append(" and r.type = "+type);
//		}
		if (!StringUtil.isBlank(roleName)) {
			hql.append(" and r.roleName like :roleName");
			param.put("roleName", "%" + roleName + "%");
		}
		if (!StringUtil.isBlank(oid)) {
		    hql.append(" and r.organization.oid = :oid" );
		    param.put("oid",oid );
		}
		hql.append(" order by r.rid desc ");
		return findPageList(hql.toString(),param, pageNo, pageSize);
	}

	public Role getRoleByName(String roleName) {
		String hql = "from Role ro where ro.roleName = ?";
		return	(Role)getFirstElement(hql,roleName);
	}

	public Pager<Role> getAllPcRole(int pageNo, int pageSize, String roleName, String oid) {
		return  getRolePage(Constants.PC, pageNo, pageSize,roleName, oid);
	}

	/**
	 * 分配角色列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Role> getRoleList() {
		String hql = "from Role r order by r.rid desc ";
		return find(hql);
	}
	
	
	/**
	 * 获取pc账号已分配角色列表
	 * @param aid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Object> getRoleOfAccount(long aid) {
		String sql = "select ar.rid from account_role ar where ar.aid = " + aid;
		return getSession().createSQLQuery(sql).list();
	}
	
	public int addAccountRole(long aid, String roleIds) {
		String sql = "insert into account_role (aid,rid,type) " +
				" select " + aid +", rid, " + Constants.PC + " from role r" +
				" where r.rid in ( " + roleIds + ")";
		return getSession().createSQLQuery(sql).executeUpdate();
	}

	public int deleteAccountRole(long aid, String roleIds) {
		String sql = "delete from account_role ar where ar.aid = " + aid + " and ar.rid in ( " + roleIds + " )";
		return getSession().createSQLQuery(sql).executeUpdate();
	}
	
	public List<Role> getAssginRoleByOrgId(String oid) {
		Map<String,Object> param = new HashMap<String,Object>();
	    String hql = "from Role r where r.organization.oid = :oid";
	    param.put("oid",oid);
	    return this.createQuery(hql, param).list();
	}
	
	/**
	 * 过滤系统角色 is_system = 0
	 * @param ids
	 * @return
	 */
	public int delRoles(String ids) {
	    int count = 0;
	    String sql = "delete from role r where r.is_system = 0 and r.rid in (" + ids + ")";
	    count = getSession().createSQLQuery(sql).executeUpdate();
	    return count;
	}
	
	public List<Role> getAssginRoleWithoutOrg() {
	    String hql = "from Role r where r.organization.oid is null ";
	    return find(hql);
	}
}